package org.gl.android.utils;


import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/***
 * @Description: des加密/解密类
 * @author net
 * @version V1.0
 * 
 */
public class Des {
	/**
	 * 加密（使用DES算法）
	 * 
	 * @param txt
	 *            需要加密的文本
	 * @param key
	 *            密钥
	 * @return 成功加密的文本
	 * @throws InvalidKeySpecException
	 * @throws InvalidKeyException
	 * @throws NoSuchPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public static String enCrypto(String txt, String key)
			throws InvalidKeySpecException, InvalidKeyException,
			NoSuchPaddingException, IllegalBlockSizeException,
			BadPaddingException {
		StringBuffer sb = new StringBuffer();
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
		SecretKeyFactory skeyFactory = null;
		Cipher cipher = null;
		try {
			skeyFactory = SecretKeyFactory.getInstance("DES");
			cipher = Cipher.getInstance("DES");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
		cipher.init(Cipher.ENCRYPT_MODE, deskey);
		byte[] cipherText =null;
		try {
			cipherText = cipher.doFinal(txt.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		for (int n = 0; n < cipherText.length; n++) {
			String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF));

			if (stmp.length() == 1) {
				sb.append("0" + stmp);
			} else {
				sb.append(stmp);
			}
		}
		return sb.toString().toUpperCase();
	}

	public static String deCrypto(String txt) {
		try {
			return deCrypto(txt, "geliang0120");
		} catch (Exception e) {
			e.printStackTrace();
			return txt;
		}
	}

	public static String enCrypto(String txt) {
		try {
			return enCrypto(txt, "geliang0120");
		} catch (Exception e) {
			e.printStackTrace();
			return txt;
		}
	}

	/**
	 * 解密（使用DES算法）
	 * 
	 * @param txt
	 *            需要解密的文本
	 * @param key
	 *            密钥
	 * @return 成功解密的文本
	 * @throws InvalidKeyException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public static String deCrypto(String txt, String key)
			throws InvalidKeyException, InvalidKeySpecException,
			NoSuchPaddingException, IllegalBlockSizeException,
			BadPaddingException {
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
		SecretKeyFactory skeyFactory = null;
		Cipher cipher = null;
		try {
			skeyFactory = SecretKeyFactory.getInstance("DES");
			cipher = Cipher.getInstance("DES");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
		cipher.init(Cipher.DECRYPT_MODE, deskey);
		byte[] btxts = new byte[txt.length() / 2];
		for (int i = 0, count = txt.length(); i < count; i += 2) {
			btxts[i / 2] = (byte) Integer.parseInt(txt.substring(i, i + 2), 16);
		}
		try {
			return (new String(cipher.doFinal(btxts),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			NoSuchPaddingException, IllegalBlockSizeException,
			BadPaddingException {
//		String soureTxt = "这里是要加密的文本内容{\"fasd\",\"fasd\"}";
//		String key = "geliang0120";
//		String str = "81D0DED0F7BA2BC10C53076DEA5D8C06780F28BAC1B7F01E8F3D758A23AADBBD241B93176A86D9B7F58FB61D8114B98D5571271463826B86F93B52B67BA60BB47A2FA33FCFC1D983C22125D3FCE20E9A4A3A414067AE7DC3B661507159B7751CD9F72990F8F6728C4B407270DAEE725363834411C080ECD45C541ECE7A4BFB6E282C2472BA2166C58196E6BD4429E9CD819FC036CE37DF46F89FD7A246BCF72C3A9E047C3DECBA0BAAF9DB96D88F28656AC79835736B9D4629BF2545E309CC63BD7545E2D7093E9F651E1FF97AC971B9E1DCCA956A3C466D0437159A2549F56FFC5798672FBB4A739426F823AE1DCE08D5A74C1934C7598CA3A113CE59095884988B58D5F6189E93F5B680FD03605C00C48C309C6E2D0FE9089C27A63E709A4B6B008E88DBCE1C7AF4B082F9E109F228F4F0816BB895F741C609431093252F59AAC40B98CACB33F6EE061E0E3931A5CEF902EC3000394B14C30D27412A38676056DFBDDC0FE03021CFC0C6EADD29E66AFA4D18601E11905BD3F8B72785842FA530B7FCB252D8DF3E03158E3ED13233DFA9370EBECFC613C35B8BAA827C0F905B5F34B4AA8D1EAE61A27AA348F91DEB217C02100D084D55BC238A0CB6A2B2201BC71E8636292AC21F4E410A78FEF56CA72C34DBB9549BC04DAB36CF9CB50C1751CF60DCCC784669E69A7FFFE00001D412400B7892C6CAE43C8156FDA8161B222723AB2D6DF7287C6B916227CA93C00FDEFB26C2CAB0744C8D2D63E3083910FF05FEE14FE8401856C7501F20CFEB7DBE470622FD9F4E153F5409B05159815C35A4C22125D3FCE20E9A4A3A414067AE7DC3B661507159B7751CD9F72990F8F6728C4B407270DAEE725363834411C080ECD471A8EEECE785131C282C2472BA2166C5FCDEABCF69020455A09950FCF897F8F5F9F8E39D516DDE94D4516A74FC17C3F74A3A414067AE7DC3E5E885E37E922B6CF5B680FD03605C00B551F93154B3F99968F2F16EEED71ACEB48A12F216D1DC8DB06E06ADE14DA536F365CF657E68780B044DC5AA7A8841383341A08D24FE82E3C6F0A0778EB3B7870756700342E8C38BB91990645AAEA44286CF9E7044C6192F6AC79835736B9D465B4D4EE4A9EF0FB39D6F74640FAA257503A7A95B0D84D8051EB2398F8B5E46D3100ED08278C3C32C34B6336F088D778A9A7FFFE00001D412400B7892C6CAE43C8156FDA8161B222723AB2D6DF7287C6B916227CA93C00FDEFB26C2CAB0744C8D2D63E3083910FF05FEE14FE8401856C7501F20CFEB7DBE479CBB422F0E84D6BC22CCB59F075F4B22C22125D3FCE20E9A4A3A414067AE7DC3B661507159B7751CD9F72990F8F6728C4B407270DAEE725363834411C080ECD489B5D981FE0173D7282C2472BA2166C5924723EFED5FC2671F203CE69ADE47C58F3D758A23AADBBD241B93176A86D9B7C2ADD4F4716C50A5A43A3D9615802DA9807D1687BB2F104DDC4C9693D0B2402E410592D3FB00305C5D6BB4B832EE5C90E645C52F7144B518A058B31FEF339A751DCF9BFC588A0714E5774BD32999A1073A9E047C3DECBA0B49995169BCEB620923AB2D6DF7287C6BA83CC3BF02527CC3BD87024D8FA76E7DD51AB1A7B8C2EDFF70EAF8281708EDA05C5FBEE0E4F4F06DFEAE20F7A5B808C8D4ADE3DD2CEF11DBD6015F9E00083089A3A113CE590958849A0F707F213AD314F455595C6887536583CD04695E35C0FB1DF346A637F26E4349561FEDF77A4A6208E65B5F23CECE904B8070053E1766CE49F088AB63BE9C39AE9671BCC2B726BE4B3296D2427D8A27D96CD226B5D46E37BBF1420F8714AE6C0A00DBA9D80124C096B241C8B234EABE634B3B9FF1A77E470E25DE6113891DD927FACFE2F6E1BBD77FE3E60DD88628F5404A1FACF64AF679FE982A99D7DE0FF68A46C0BAB6FD5FE3DAC80215475157AFF89FD7A246BCF72C3A9E047C3DECBA0BAAF9DB96D88F28656AC79835736B9D4629BF2545E309CC63BD7545E2D7093E9F651E1FF97AC971B9E1DCCA956A3C466D0437159A2549F56F4EA7CB657C8E4242016D1EF3888772BBD5A74C1934C7598CA3A113CE59095884988B58D5F6189E93F5B680FD03605C00C48C309C6E2D0FE970CCDE9789154A7C2771D8F9E6329129CF6D5EC7E27A88F723AB1C9EC8878ACA3C8F3E2B4E2DB8B24047E05E08AB14E04A3A414067AE7DC3E5E885E37E922B6CF5B680FD03605C00ACDC14E1BE1A98D207B0FE12DCED588CEB463CA71D02824C7C8A8E38C092C697EE061E0E3931A5CEEFAEDF0C87BB6E29F455595C68875365683B4F23E1A1E8F8EE24A40802D1482CD0E11FC8E501E43ECF6D5EC7E27A88F7A184DE4048869ADB3D6D0B9E31268F409A7FFFE00001D412400B7892C6CAE43C8156FDA8161B222723AB2D6DF7287C6B916227CA93C00FDEFB26C2CAB0744C8D2D63E3083910FF05FEE14FE8401856C7501F20CFEB7DBE47C74735A1FF4A7D7D7C876FE1A96B9D44C22125D3FCE20E9A4A3A414067AE7DC3B661507159B7751CD9F72990F8F6728C4B407270DAEE7253B6BED73E9BD5BEF00362D2D61A86CEA1282C2472BA2166C51FBD8DB3AE0894B4385E09BEF1835ECA4300D53023245FB99A7FFFE00001D412400B7892C6CAE43C8156FDA8161B222723AB2D6DF7287C6B916227CA93C00FDEFB26C2CAB0744C8D2D63E3083910FF05FEE14FE8401856C7501F20CFEB7DBE47B382E5B1BA9AAE21C6572ABED18BB061C22125D3FCE20E9A4A3A414067AE7DC3B661507159B7751CD9F72990F8F6728C4B407270DAEE7253B6BED73E9BD5BEF07E96D5874F8CF19D282C2472BA2166C59EC0ECE281C8AC54B5A5B55284F79C8F23FAA16098691D3AA3A113CE59095884F791867B6988C06AF455595C6887536583CD04695E35C0FB1DF346A637F26E4349561FEDF77A4A6208E65B5F23CECE904B8070053E1766CE01175027342A0154E92B1CF44A3224844B3296D2427D8A27D96CD226B5D46E37BBF1420F8714AE6C0A00DBA9D80124C096B241C8B234EABE634B3B9FF1A77E47B7F3ED1275D9E26DBC42D44153AB4BE5F1291EA0CDD230891B994CF8EE68C8C060C209000FB2DD6A8F3D758A23AADBBD241B93176A86D9B7F58FB61D8114B98D5571271463826B86BCCA1CF4C71A6BE1E9C7AFC10C776AA6D5A74C1934C7598CA3A113CE59095884988B58D5F6189E93F5B680FD03605C00C48C309C6E2D0FE9C38271B07DD390104BA0349C06BFB46CCF6D5EC7E27A88F70A8D516B4C0A42C5D908CADE63E0AF718C55288F8D94482C11105F61A8D23DA237C4E731395A27CF8F1A91FCBE6A656E68592A9D3FDC3E673439F865A57324A43E0B4CAA065C868C57B225C4E5F903E7C1E68193FB80067BE5F55FA5F4602334F7DF7DE52F4DA61CF54A28AF2947506ABDA00358642AED8875FDBE41639E6352400B7892C6CAE43CE58813BD4050148CC30D27412A3867602F67B10AEC5ED999B2CE94B6D0351C3BFD2B88DA75A98BA410EEE1CDC52568B1F0903CDFE0C9DB791463DC8DB23ABC30A3F74792ACB4452EA3A113CE590958849A0F707F213AD314F455595C6887536583CD04695E35C0FB1DF346A637F26E4349561FEDF77A4A6208E65B5F23CECE904B8070053E1766CE01175027342A015446B8CB654C699FE34B3296D2427D8A27D96CD226B5D46E37BBF1420F8714AE6C0A00DBA9D80124C096B241C8B234EABE634B3B9FF1A77E4794D71D9B3B7CDA9F27FACFE2F6E1BBD781E546C3C2F67FB8A7561529B4CAE8980B1FA1AC015BC9B897377D0CFBC394A1A3A113CE59095884D303C23D33334DBCF455595C6887536583CD04695E35C0FB1DF346A637F26E4349561FEDF77A4A6208E65B5F23CECE904B8070053E1766CEF6310338A1E37E76EF8B5E70FAEECB144B3296D2427D8A27D96CD226B5D46E37BBF1420F8714AE6C0A00DBA9D80124C096B241C8B234EABE634B3B9FF1A77E470A8C95CB99BE1FB5D23ADBFEFCC271AC39664295CFD7BBE0864676DFE32D9E8F0BBFBEEAF9C01786F89FD7A246BCF72C3A9E047C3DECBA0B754938BB541A4B606AC79835736B9D4629BF2545E309CC63BD7545E2D7093E9F651E1FF97AC971B9E1DCCA956A3C466D0437159A2549F56F60B5223F2ADD901E9925F945ADE45D24D5A74C1934C7598CA3A113CE59095884988B58D5F6189E93F5B680FD03605C00C48C309C6E2D0FE9089C27A63E709A4B5191F8C64DDF0F7CF4B082F9E109F2287E169A9B17DD818286AC1FCFB191945BAAC40B98CACB33F6EE061E0E3931A5CEF902EC3000394B14C30D27412A38676056DFBDDC0FE03021CFC0C6EADD29E66AFA4D18601E11905BD3F8B72785842FA530B7FCB252D8DF3EFB2731156892A3B171BD4C0FA1A7C3325B8BAA827C0F905B5F34B4AA8D1EAE61A27AA348F91DEB217C02100D084D55BC238A0CB6A2B2201BC71E8636292AC21F6746965DAF0909682C34DBB9549BC04DAB36CF9CB50C17511947695EBCAD1A906627F72E1B5B1A664A3A414067AE7DC3E5E885E37E922B6CF5B680FD03605C00ACDC14E1BE1A98D2D6E59CE36EF9736607205CB78D19CF72D5A74C1934C7598CA3A113CE59095884988B58D5F6189E93F5B680FD03605C00C48C309C6E2D0FE94A10B372D78BD02B6BA404FCD5B75874F4B082F9E109F2282B9AED65AE585A28F5A228C1B1AEC411AAC40B98CACB33F6EE061E0E3931A5CEF902EC3000394B14C30D27412A38676056DFBDDC0FE03021CFC0C6EADD29E66AFA4D18601E11905BD3F8B72785842FA530B7FCB252D8DF3EB60C5A883FFFFEF8D865F2D92E4B9D915B8BAA827C0F905B5F34B4AA8D1EAE61A27AA348F91DEB217C02100D084D55BC238A0CB6A2B2201BC71E8636292AC21F8B350E6F5406EDB62C34DBB9549BC04DCF028BEC56ED15A3A2187283B938A102A6EB99E466005E864A3A414067AE7DC3E5E885E37E922B6CF5B680FD03605C00ACDC14E1BE1A98D2EE1E5557B5B7E17251E2A704ED631DB1D5A74C1934C7598CA3A113CE59095884988B58D5F6189E93F5B680FD03605C00C48C309C6E2D0FE9089C27A63E709A4B09214799A05157F9F4B082F9E109F2288B2231BA3D3E4A33AAC40B98CACB33F6EE061E0E3931A5CEF902EC3000394B14C30D27412A38676056DFBDDC0FE03021CFC0C6EADD29E66AFA4D18601E11905BD3F8B72785842FA530B7FCB252D8DF3EAEC43691842A92355A27F33D673C51925B8BAA827C0F905B5F34B4AA8D1EAE61A27AA348F91DEB217C02100D084D55BC238A0CB6A2B2201BC71E8636292AC21F976B2E43E3D23FC72C34DBB9549BC04D385F70BE68CE8770AB95224CC983D8359A7FFFE00001D412400B7892C6CAE43C8156FDA8161B222723AB2D6DF7287C6B916227CA93C00FDEFB26C2CAB0744C8D2D63E3083910FF05FEE14FE8401856C7501F20CFEB7DBE472076B7BD92929BBFEB6F53DC5EF20B93C22125D3FCE20E9A4A3A414067AE7DC3B661507159B7751CD9F72990F8F6728C4B407270DAEE725363834411C080ECD4B42BF6E3890955A6282C2472BA2166C52C41DF142AB9D5B0E5DAA7AB47D3D3E0E0945215173F453DA3A113CE590958849A0F707F213AD314F455595C688753657CFA22C36367084F76BE99D17D0EE2F05DC32F9E297EBADF7C8A8E38C092C697EE061E0E3931A5CEEFAEDF0C87BB6E29F455595C68875365683B4F23E1A1E8F850747526CDAC10F836665DAC22A884C235DDC643C7B83939";
//		System.out.println("明文：" + soureTxt);
////		str = enCrypto(soureTxt, key);
//		System.out.println("加密：" + str);
//		System.out.println("解密：" + deCrypto(str, key));
////		System.out.println(str + "====" + key);

	}
}